Imports System.Data.SqlClient

Public Class DanhPhap
    Private nMa As Integer
    Private sMavt As String
    Private sTenvt As String

    Public Property Ma() As Integer
        Get
            Return nMa
        End Get
        Set(ByVal Value As Integer)
            nMa = Value
        End Set
    End Property

    Public Property Mavt() As String
        Get
            Return sMavt
        End Get
        Set(ByVal Value As String)
            sMavt = Value
        End Set
    End Property

    Public Property Tenvt() As String
        Get
            Return sTenvt
        End Get
        Set(ByVal Value As String)
            sTenvt = Value
        End Set
    End Property

End Class

Public Class DanhPhapF

    Public Function GetDmVatTu(ByVal Mavt As String) As DataTable

        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhPhap_GetDmVatTu"

            .Parameters.Add(New SqlClient.SqlParameter("@mavt", SqlDbType.Char))
            .Parameters("@mavt").Value = Mavt
        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
        myCnn.Dispose()

    End Function

    Public Function GetAll() As DataTable
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhPhap_GetAll"

        End With

        myDat = New SqlDataAdapter(myCmd)
        myDat.Fill(myDtb)
        Return myDtb
        myCnn.Close()
    End Function

    Public Function ExistsMavt(ByVal Mavt As Integer) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhPhap_ExistsMavt"

            .Parameters.Add(New SqlClient.SqlParameter("@mavt", SqlDbType.Int))
            .Parameters("@mavt").Value = Mavt

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Insert(ByVal DanhPhap As DanhPhap) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhPhap_Insert"

            .Parameters.Add(New SqlClient.SqlParameter("@ma", SqlDbType.Int))
            .Parameters("@ma").Value = DanhPhap.Ma

            .Parameters.Add(New SqlClient.SqlParameter("@mavt", SqlDbType.Char))
            .Parameters("@mavt").Value = DanhPhap.Mavt

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Update(ByVal DanhPhap As DanhPhap, ByVal _Mavt As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhPhap_Update"

            .Parameters.Add(New SqlClient.SqlParameter("@_mavt", SqlDbType.NVarChar))
            .Parameters("@_mavt").Value = _Mavt

            .Parameters.Add(New SqlClient.SqlParameter("@ma", SqlDbType.Int))
            .Parameters("@ma").Value = DanhPhap.Ma

            .Parameters.Add(New SqlClient.SqlParameter("@mavt", SqlDbType.Char))
            .Parameters("@mavt").Value = DanhPhap.Mavt

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function Delete(ByVal Mavt As String) As Boolean
        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhPhap_Delete"

            .Parameters.Add(New SqlClient.SqlParameter("@mavt", SqlDbType.Char))
            .Parameters("@mavt").Value = Mavt

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If
    End Function

    Public Function UpdateStore() As Boolean

        Dim myProjCnn As New ProjectConnection
        Dim myCnn As New SqlConnection
        Dim myDat As New SqlDataAdapter
        Dim myDtb As New DataTable

        myCnn = myProjCnn.GetConnection
        myCnn.Open()

        Dim myCmd As New SqlCommand
        With myCmd
            .Connection = myCnn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "DanhPhap_UpdateStore"

            .Parameters.Add(New SqlClient.SqlParameter("@return", SqlDbType.Bit)).Direction = ParameterDirection.ReturnValue
            .ExecuteNonQuery()
        End With

        If myCmd.Parameters("@return").Value = 1 Then
            myCnn.Close()
            myCnn.Dispose()
            Return True
        Else
            myCnn.Close()
            myCnn.Dispose()
            Return False
        End If

    End Function

End Class




